

#import "BaseButton.h"

@implementation BaseButton
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.width==0) {
        self.width=10;
    }
    
    if (self.leftOrRightOrTopOrDown==0) {
        /** 修改 title 的 frame */
        // 1.获取 titleLabel 的 frame
        CGRect titleLabelFrame = self.titleLabel.frame;
        // 2.修改 titleLabel 的 frame
        /** 修改 imageView 的 frame */
        // 1.获取 imageView 的 frame
        CGRect imageViewFrame = self.imageView.frame;
    
        if (self.btnAlignment==0) {
            titleLabelFrame.origin.x = 0;
        }else if (self.btnAlignment==1) {
            titleLabelFrame.origin.x = (self.frame.size.width - titleLabelFrame.size.width-self.width-imageViewFrame.size.width)/2;
        }else if (self.btnAlignment==2) {
            titleLabelFrame.origin.x =self.frame.size.width - titleLabelFrame.size.width-self.width-imageViewFrame.size.width;
        }
        // 3.重新赋值
        self.titleLabel.frame = titleLabelFrame;
        // 3.重新赋值
        self.imageView.frame = imageViewFrame;
    }else if(self.leftOrRightOrTopOrDown==1){
        /** 修改 title 的 frame */
        // 1.获取 titleLabel 的 frame
        CGRect titleLabelFrame = self.titleLabel.frame;
        // 2.修改 titleLabel 的 frame
        /** 修改 imageView 的 frame */
        // 1.获取 imageView 的 frame
        CGRect imageViewFrame = self.imageView.frame;
    
        if (self.btnAlignment==0) {
            imageViewFrame.origin.x = 0;
        }else if (self.btnAlignment==1) {
            imageViewFrame.origin.x = (self.frame.size.width - titleLabelFrame.size.width-self.width-imageViewFrame.size.width)/2;
        }else if (self.btnAlignment==2) {
            imageViewFrame.origin.x =self.frame.size.width - titleLabelFrame.size.width-self.width-imageViewFrame.size.width;
        }
        // 3.重新赋值
        self.titleLabel.frame = titleLabelFrame;
        // 3.重新赋值
        self.imageView.frame = imageViewFrame;
    }else if (self.leftOrRightOrTopOrDown==2) {
        /** 修改 title 的 frame */
        // 1.获取 titleLabel 的 frame
        CGRect titleLabelFrame = self.titleLabel.frame;
        /** 修改 imageView 的 frame */
        // 1.获取 imageView 的 frame
        CGRect imageViewFrame = self.imageView.frame;
        // 2.修改 titleLabel 的 frame
        titleLabelFrame.origin.y = (self.frame.size.height- titleLabelFrame.size.height-imageViewFrame.size.height-self.width)/2;
        // 2.修改 imageView 的 frame
        imageViewFrame.origin.y = titleLabelFrame.origin.y+titleLabelFrame.size.height+self.width;
        if (self.btnAlignment==0) {
            titleLabelFrame.origin.x=0;
            imageViewFrame.origin.x=0;
        }else  if (self.btnAlignment==1) {
            titleLabelFrame.origin.x=self.frame.size.width/2-titleLabelFrame.size.width/2;
            imageViewFrame.origin.x=self.frame.size.width/2-imageViewFrame.size.width/2;
        }else  if (self.btnAlignment==2) {
            titleLabelFrame.origin.x=self.frame.size.width-titleLabelFrame.size.width;
            imageViewFrame.origin.x=self.frame.size.width-imageViewFrame.size.width;
        }
        // 3.重新赋值
        self.titleLabel.frame = titleLabelFrame;
        // 3.重新赋值
        self.imageView.frame = imageViewFrame;
        
    }else if(self.leftOrRightOrTopOrDown==3){
        /** 修改 title 的 frame */
        // 1.获取 titleLabel 的 frame
        CGRect titleLabelFrame = self.titleLabel.frame;
        /** 修改 imageView 的 frame */
        // 1.获取 imageView 的 frame
        CGRect imageViewFrame = self.imageView.frame;
        // 2.修改 titleLabel 的 frame
        titleLabelFrame.origin.y = titleLabelFrame.origin.y+titleLabelFrame.size.height+self.width;
        // 2.修改 imageView 的 frame
        imageViewFrame.origin.y = (self.frame.size.height- titleLabelFrame.size.height-imageViewFrame.size.height-self.width)/2;
        if (self.btnAlignment==0) {
            titleLabelFrame.origin.x=0;
            imageViewFrame.origin.x=0;
        }else  if (self.btnAlignment==1) {
            titleLabelFrame.origin.x=self.frame.size.width/2-titleLabelFrame.size.width/2;
            imageViewFrame.origin.x=self.frame.size.width/2-imageViewFrame.size.width/2;
        }else  if (self.btnAlignment==2) {
            titleLabelFrame.origin.x=self.frame.size.width-titleLabelFrame.size.width;
            imageViewFrame.origin.x=self.frame.size.width-imageViewFrame.size.width;
        }
        // 3.重新赋值
        self.titleLabel.frame = titleLabelFrame;
        // 3.重新赋值
        self.imageView.frame = imageViewFrame;
       
    }
    
   
}

@end
